package com.zy.service.impl;


import com.alibaba.dubbo.config.annotation.Service;
import com.zy.dao.OrderSettingDao;
import com.zy.pojo.OrderSetting;
import com.zy.service.OrderSettingService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.net.MalformedURLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service(interfaceClass=OrderSettingService.class)
@Transactional
public class OrderSettingServiceImpl implements OrderSettingService {

	@Autowired
	private OrderSettingDao orderSettingDao;

	@Override
	public void add(ArrayList<OrderSetting> orderSettings) {
		if(orderSettings!=null && orderSettings.size() > 0){
			for (OrderSetting orderSetting : orderSettings) {
				//先判断数据库中有没有
				Integer count=orderSettingDao.findCountByOrderDate(orderSetting.getOrderDate());
				if(count>0){
					//有---修改
					orderSettingDao.edit(orderSetting);
				}else{
					//无---添加
					orderSettingDao.add(orderSetting);
				}
			}
		}
	}

	@Override
	public List<Map> getOrderSettingByMonth(String string)  {
		//开始日期
		String dateBegin =string+"-1";
		//结束日期计算（每月结束日期计算）
		String dateEnd = string+"-31";
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
			Date dBegin = sdf.parse(string);
			dBegin.setMonth(dBegin.getMonth()+1); //月加一
			dBegin.setDate(dBegin.getDate()-1); //日减一
			SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
			dateEnd = sdf1.format(dBegin);
//			System.out.println(dateEnd);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		//查询两个日期之间的数据
		List<OrderSetting> list=orderSettingDao.getOrderSettingByMonth(dateBegin,dateEnd);
		//封装为List<Map>集合
		List<Map> res = new ArrayList<>();
		for (OrderSetting orderSetting : list) {
			Map map = new HashMap<>();
			map.put("date",orderSetting.getOrderDate().getDate());//获得日期
			map.put("number",orderSetting.getNumber());//可预约人数
			map.put("reservations",orderSetting.getReservations());//已预约人数
			res.add(map);
		}
		return res;
	}

	@Override
	public void editNumberByDate(OrderSetting orderSetting) {
		ArrayList<OrderSetting> list = new ArrayList<>();
		//修改数据的时候，也要进行判断使用这条数据已经录入过数据库
		//有--修改
		//无--添加
		list.add(orderSetting);
		this.add(list);
		System.out.println(11111);
	}
}
